package com.example.geek.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.geek.R;
import com.example.geek.entity.PostEntity;
import com.example.geek.listener.OnItemClickListener;
import com.example.geek.view.CircleTransform;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import com.example.geek.view.CircleTransform;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.util.List;

public class PostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<PostEntity> datas;
    private static OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setDatas(List<PostEntity> datas) {
        this.datas = datas;
    }

    public PostAdapter(Context context) {
        this.mContext = context;
    }

    public PostAdapter(Context context, List<PostEntity> datas) {
        this.mContext = context;
        this.datas = datas;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_post_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        PostEntity postEntity = datas.get(position);
        vh.tvTitle.setText(postEntity.getVtitle());
        vh.tvAuthor.setText(postEntity.getAuthor());
        vh.tvComment.setText(String.valueOf(postEntity.getCommentNum()));
        vh.tvShare.setText(String.valueOf(postEntity.getCollectNum()));
        vh.tvDz.setText(String.valueOf(postEntity.getLikeNum()));
        vh.postEntity = postEntity;

        Picasso.with(mContext)
                .load(postEntity.getHeadurl())
                .transform(new CircleTransform())
                .into(vh.imgHeader);
    }

    @Override
    public int getItemCount() {
        if (datas != null && datas.size() > 0) {
            return datas.size();
        } else {
            return 0;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private TextView tvAuthor;
        private TextView tvComment;
        private TextView tvShare;
        private TextView tvDz;
        private ImageView imgHeader;
        private PostEntity postEntity;
        public ViewHolder(@NonNull View view) {
            super(view);
            tvTitle = view.findViewById(R.id.title);
            tvAuthor = view.findViewById(R.id.author);
            tvComment = view.findViewById(R.id.comment);
            tvShare = view.findViewById(R.id.share);
            tvDz = view.findViewById(R.id.dz);
            imgHeader = view.findViewById(R.id.img_header);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(postEntity);
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Serializable obj);
    }
}
